using System;
using dbvc.data;
using dbvc.Queries;

namespace dbvc.context.Version
{
    public static partial class VersionRole
    {
        public static void Save(this db_version self)
        {
            GenericQuery<db_version>.Create(self, (o, i) => o.id = i);
        }

        public static db_version For(this db_version self, db_project prj)
        {
            self.id_project = prj.id;
            return self;
        }
    }

    public static class ActivityRole
    {
        public static void Save(this db_activity self)
        {
            GenericQuery<db_activity>.Create(self);
        }
    }

    public class CreateVersion
    {
        private readonly db_activity Activity;
        private readonly db_version Version;
        private readonly db_user principal;
        private readonly db_project project;

        public CreateVersion(db_user _principal, db_project _project, db_version _version)
        {
            principal = _principal;
            project = _project;
            Version = _version;
            Activity = new db_activity {action = "add version", datestamp = DateTime.Now, userstamp = principal.name};
        }

        public CreateVersion(db_user _principal, int _project, db_version _version)
        {
            principal = _principal;
            project = new db_project() {id = _project};
            Version = _version;
            Activity = new db_activity { action = "add version", datestamp = DateTime.Now, userstamp = principal.name };
        }

        public db_version Execute()
        {
            Version.For(project).Save();
            Activity.Save();

            return Version;
        }
    }
}